package cn.tedu.tmall.front.mall.contorller;

import cn.tedu.tamll.common.pojo.CurrentUser;
import cn.tedu.tamll.common.web.JsonResult;
import cn.tedu.tmall.front.mall.pojo.vo.CartVO;
import cn.tedu.tmall.front.mall.service.ICartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@Api(tags = "购物车模块")
@Slf4j
@Validated
@RestController
@RequestMapping("/carts")
public class CartController {
    @Autowired
    private ICartService cartService;

    /**
     * 增加商品到购物车
     * @return
     */
    @ApiOperation(value = "添加商品")
    @PostMapping("add")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name="goodsId",value = "商品id",required = true,dataType = "Long"),
                    @ApiImplicitParam(name="goodsNumber",value = "商品数量",required = true,dataType = "Integer"),
            }
    )
    public JsonResult add(@ApiIgnore @AuthenticationPrincipal CurrentUser user,
                          @Range(min = 1,message = "请输入合法的商品id") Long goodsId,
                          @Range(min = 1,max = 100,message = "商品数量必须到1-100之间") Integer goodsNumber) {
        cartService.add(user,goodsId,goodsNumber);
        return JsonResult.ok();
    }
    /**
     * 删除商品到购物车
     * @return
     */
    @ApiOperation(value = "删除商品")
    @PostMapping("delete")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name="goodsId",value = "商品id",required = true,dataType = "Long")
            }
    )
    public JsonResult delete(@ApiIgnore @AuthenticationPrincipal CurrentUser user,
                         @RequestParam @Range(min = 1,message = "请输入合法的商品id") Long goodsId) {
        Long userId = user.getUserId();
        cartService.delete(userId,goodsId);
        return JsonResult.ok();
    }
    @GetMapping("/list")
    @ApiOperation(value = "我的购物车")
    public JsonResult list (@ApiIgnore @AuthenticationPrincipal CurrentUser user) {
        List<CartVO> cartVOS=  cartService.list(user.getUserId());
        return JsonResult.ok(cartVOS);
    }
//
    @ApiOperation(value = "修改商品")
    @PostMapping("modify")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name="goodsId",value = "商品id",required = true,dataType = "Long"),
                    @ApiImplicitParam(name="newGoodsNumber",value = "商品数量",required = true,dataType = "Integer"),
            }
    )
    public  JsonResult modify(@ApiIgnore @AuthenticationPrincipal CurrentUser user,
                              @Range(min = 1,message = "请输入合法的商品id") Long goodsId,
                              @Range(min = 1,max = 100,message = "商品数量必须到1-100之间") Integer newGoodsNumber) {
        log.debug("NewGoodsNumber:{}",newGoodsNumber);
        cartService.modify(user.getUserId(),goodsId,newGoodsNumber);
        return JsonResult.ok();
    }

    @ApiOperation(value = "增加数量")
    @PostMapping("incr")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name="goodsId",value = "商品id",required = true,dataType = "Long"),
                    @ApiImplicitParam(name="goodsNumber",value = "商品数量",required = true,dataType = "Integer"),
            }
    )
    public  JsonResult incr(@ApiIgnore @AuthenticationPrincipal CurrentUser user,
                              @Range(min = 1,message = "请输入合法的商品id") Long goodsId,
                              @Range(min = 1,max = 100,message = "商品数量必须到1-100之间") Integer goodsNumber) {
        cartService.incr(user.getUserId(),goodsId,goodsNumber);
        return JsonResult.ok();
    }

    @ApiOperation(value = "减少数量")
    @PostMapping("reduce")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name="goodsId",value = "商品id",required = true,dataType = "Long"),
                    @ApiImplicitParam(name="goodsNumber",value = "商品数量",required = true,dataType = "Integer"),
            }
    )
    public  JsonResult reduce(@ApiIgnore @AuthenticationPrincipal CurrentUser user,
                            @Range(min = 1,message = "请输入合法的商品id") Long goodsId,
                            @Range(min = 1,max = 100,message = "商品数量必须到1-100之间")  Integer goodsNumber) {
        cartService.reduce(user.getUserId(),goodsId,goodsNumber);
        return JsonResult.ok();
    }


    @ApiOperation(value = "选中商品")
    @PostMapping("pick/on")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name="goodsId",value = "商品id",required = true,dataType = "Long"),

            }
    )
    public  JsonResult isPick(@ApiIgnore @AuthenticationPrincipal CurrentUser user,
                             @RequestParam @Range(min = 1,message = "请输入合法的商品id") Long goodsId) {
        cartService.pick(user.getUserId(),goodsId);
        return JsonResult.ok();
    }

    @ApiOperation(value = "取消选中商品")
    @PostMapping("pickOff/on")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name="goodsId",value = "商品id",required = true,dataType = "Long"),

            }
    )
    public  JsonResult pickOff(@ApiIgnore @AuthenticationPrincipal CurrentUser user,
                               @RequestParam  @Range(min = 1,message = "请输入合法的商品id") Long goodsId) {
        cartService.pickOff(user.getUserId(),goodsId);
        return JsonResult.ok();
    }

}
